package com.luatos.iot.regproxy.module;

import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.lang.util.Regex;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Ok;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.iotexplorer.v20190423.IotexplorerClient;
import com.tencentcloudapi.iotexplorer.v20190423.models.CreateDeviceRequest;
import com.tencentcloudapi.iotexplorer.v20190423.models.CreateDeviceResponse;
import com.tencentcloudapi.iotexplorer.v20190423.models.GetDeviceListRequest;
import com.tencentcloudapi.iotexplorer.v20190423.models.GetDeviceListResponse;

// 腾讯云物联网注册服务
@At("/reg")
@Fail("http:500")
@IocBean
public class QcloudIotRegModule {

	private static Log log = Logs.get();

	@Inject
	protected PropertiesProxy conf;

	@At
	@Ok("json:tidy")
	public NutMap qcloud(String dev, String key, String sign, String ranstr, String secretId, String secretKey, String region) {
		// 首先, 检查必要的参数
		if (Strings.isBlank(dev) || Strings.isBlank(key)) {
			log.debug("miss dev/key/sign/ranstr");
			return new NutMap("msg", "miss dev/key/sign");
		}
		if (!Regex.match("^[a-zA-z0-9]{10,20}", dev)) {
			log.info("非法的设备名称" + dev);
			return new NutMap("msg", "bad dev name");
		}

		// 补齐参数

		if (Strings.isBlank(secretId))
			secretId = conf.get("luatos.regproxy.qcloud." + key + ".secretId");
		if (Strings.isBlank(secretKey))
			secretKey = conf.get("luatos.regproxy.qcloud." + key + ".secretKey");
//		if (Strings.isBlank(token))
//			token = conf.get("luatos.regproxy.qcloud." + key + ".token");
		if (Strings.isBlank(region))
			secretId = conf.get("luatos.regproxy.qcloud." + region + ".secretId", "ap-guangzhou");

		if (Strings.isBlank(secretId))
			return new NutMap("msg", "miss secretId");
		if (Strings.isBlank(secretKey))
			return new NutMap("msg", "miss secretKey");
//		if (Strings.isBlank(token))
//			return new NutMap("msg", "miss token");
//		if (Strings.isBlank(region))
//			return new NutMap("msg", "miss region");

		Credential credential = new Credential(secretId, secretKey);
		IotexplorerClient client = new IotexplorerClient(credential, region);

		try {
			GetDeviceListRequest getDeviceListRequest = new GetDeviceListRequest();
			getDeviceListRequest.setDeviceName(dev);
			getDeviceListRequest.setProductId(key);
			GetDeviceListResponse getDeviceListResponse = client.GetDeviceList(getDeviceListRequest);
			if (getDeviceListResponse.getTotal() != null && getDeviceListResponse.getTotal() > 0) {
				String secret = getDeviceListResponse.getDevices()[0].getDevicePsk();
				return new NutMap("secret", secret);
			}

			// 需要新建设备了
			CreateDeviceRequest createDeviceRequest = new CreateDeviceRequest();
			createDeviceRequest.setDeviceName(dev);
			createDeviceRequest.setProductId(key);

			CreateDeviceResponse createDeviceResponse = client.CreateDevice(createDeviceRequest);
			if (createDeviceResponse.getData() != null) {
				return new NutMap("secret", createDeviceResponse.getData().getDevicePsk());
			}
		} catch (TencentCloudSDKException e) {
			return new NutMap("msg", "sdk error " + e.toString());
		}

		return new NutMap("msg", "sdk error");
	}
}
